﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApp.BLL.Base;
using WebApp.DAL;
using WebApp.Models;
using WebApp.Common;
using System.Configuration;

namespace WebApp.BLL.Manage
{
    public class S_user_validatenumService : BaseService<S_user_validatenum>
    {
        public enum enmValidateType
        {
            register,
            forgotpassword
        }

        #region DAL
        #endregion

        #region 构造
        public S_user_validatenumService() : base(new S_user_validatenumRepository()) { }

        #endregion

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <param name="username"></param>
        /// <param name="mobile">手机号</param>
        /// <param name="type">验证码类型（register/forgotpassword）</param>
        /// <param name="expiremins">过期时间，单位：分钟（默认30分钟）</param>
        /// <returns></returns>
        public S_user_validatenum GetValidateCode(string username, string mobile, string type, out string errmsg, int expiremins = 30)
        {
            errmsg = "";

            S_user_validatenum item = this.CurrentRepository.Entities.FirstOrDefault(p => p.Mobilenum == mobile && p.Validatetype == type);
            if (item == null)
            {
                //生成新验证码
                item = new S_user_validatenum();
                item.Username = username;
                item.Mobilenum = mobile;
                item.Validatetype = type;
                item.Validatenum = new ValidateCode().CreateNumberVerifyCode(4);
                item.Createtime = DateTime.Now;
                item.Expiretime = item.Createtime.AddMinutes(expiremins);
                this.CurrentRepository.Add(item);
            }
            else if (item.Expiretime <= DateTime.Now)
            {
                //验证码已过期，重新发送
                item.Validatenum = new ValidateCode().CreateNumberVerifyCode(4);
                item.Createtime = DateTime.Now;
                item.Expiretime = item.Createtime.AddMinutes(expiremins);
                this.CurrentRepository.Update(item);
            }
            else
            {
                //测试阶段，暂取消限制
                ////验证码未过期
                //if (item.Createtime > DateTime.Now.AddMinutes(-1))
                //{
                //    //一分钟内不允许重复获取
                //    errmsg = "您操作太快了，请等会再发~";
                //    return null;
                //}
                //else
                //{
                //如果超过一分钟，不需要重新生成验证码直接发送，仅更新验证码有效期
                item.Createtime = DateTime.Now;
                item.Expiretime = item.Createtime.AddMinutes(expiremins);
                this.CurrentRepository.Update(item);
                //}
            }

            return item;
        }

        /// <summary>
        /// 验证码是否正确，且未过期
        /// </summary>
        /// <param name="username"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool isRight(string mobile, string type, string code)
        {
            S_user_validatenum validate = this.CurrentRepository.Entities.FirstOrDefault(p => p.Mobilenum == mobile && p.Validatetype == type);
            if (validate == null)
            {
                return false;
            }
            else if (validate.Validatenum == code && validate.Expiretime >= DateTime.Now)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
